<?php

class Spotlights_ajax extends Admin_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->sSkinFile = 'ajax';
	}
	
	public function index()
	{
		return null;
	}
	
	
	public function get_spotlights_json()
	{
		
		$iSpotlightId = $this->input->post('spotlight_id', true, 0);

		$this->load->model('db/views/spotlights_by_site', 'vw_spotlights_by_site');
		
		if ($this->oSecGroup->site_id !== null)
		{
			$this->vw_spotlights_by_site->site_id = $this->oSecGroup->site_id;
		}
		
		if ($iSpotlightId !== 0)
		{
			$this->vw_spotlights_by_site->spotlight_id = $iSpotlightId;
		}
		else 
		{
			$this->vw_spotlights_by_site->version_active = 'Y';
		}
		
		$qSpotlights = $this->vw_spotlights_by_site->get();
		
		$this->aData['qSpotlights'] = $qSpotlights;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/spotlights/ajax/row_data_json', $this->aData);
	}
	
	
	
	public function search_spotlights()
	{
		$sSearchText = $this->input->post('sSearchText', true, '');
		$iSearchSite = (int) $this->input->post('iSearchSite', true, 0);
	
		$this->load->model('db/views/spotlights_by_site', 'vw_spotlights_by_site');

		
		if ($iSearchSite !== 0)
		{
			$this->vw_spotlights_by_site->site_id = $iSearchSite;	
		}
		

		$this->vw_spotlights_by_site->version_active = 'Y';

		$aOptions = array();
		$aOptions[] = array('like' => array(
										'spotlight_title', $sSearchText, 'both', 0, 0 
		));
		
		$qSpotlights = $this->vw_spotlights_by_site->get($aOptions);
		
		$this->aData['qSpotlights'] = $qSpotlights;		
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/spotlights/ajax/row_data_json', $this->aData);
		return;
	}
	
	
	public function get_spotlight_image_template()
	{
	
		$this->load->model('db/views/spotlights_by_site', 'vw_spotlights_by_site');

		$oSpotlight = $this->vw_spotlights_by_site->get_object();

		
		$this->load->model('db/views/spotlight_images', 'vw_spotlight_images');
		
		$oSpotlightImages = $this->vw_spotlight_images->get_object();
		
		
		$this->load->model('db/views/spotlight_text', 'vw_spotlight_text');

		$aSpotlightText = array();

		for ($i = 0; $i < $this->config->item('spotlight_images_text_count'); $i++)
		{
			$aSpotlightText[$i] = $this->vw_spotlight_text->get_object();
		}
		
		
		
		$this->aData['oSpotlight'] = $oSpotlight;
		$this->aData['oSpotlightImages'] = $oSpotlightImages;
		$this->aData['aSpotlightText'] = $aSpotlightText;
		
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/spotlights/spotlight_image_template', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent, false);
		
		$this->aData['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $this->aData);
	}



	public function get_version_number()
	{
		$iVersionId = (int) $this->input->post('iVersionId', true, 0);
		$sSpotlightTitle = $this->input->post('sSpotlightTitle', true, '');
		$sVersionActive = $this->input->post('sVersionActive', true, 'N'); 
		
		// we'll always need this model		
		$this->load->model('db/spotlight_versions', 'spotlight_versions');
		
		$iVersionNum = 1;
		
		// if we don't have a version, then it's a new spotlight
		if ($iVersionId === 0) 
		{
			$this->load->model('db/spotlights', 'spotlights');
		
			$this->spotlights->site_id = 1; // TODO: get the actual site ID
			$this->spotlights->spotlight_title = $sSpotlightTitle;
			
			$iSpotlightId = $this->spotlights->save();
		}
		else  // if we've got a version, we need to get the spotlight
		{
			$this->spotlight_versions->version_id = $iVersionId;
			
			$qSpotlightVersion = $this->spotlight_versions->get(1);
			
			
			if ($qSpotlightVersion->num_rows() === 0)
			{
				// TODO: Throw an error, as it's not a valid version_id	
				return;
			}
			
			$iVersionNum = $qSpotlightVersion->row(0)->version_num + 1;
			$iSpotlightId = $qSpotlightVersion->row(0)->spotlight_id;
			// clear the values as we're about to save
			$this->spotlight_versions->clear();
			
			// if we're activating this version, we need to deactivate all other versions of this spotlight
			if ($sVersionActive === 'Y') 
			{
				$this->spotlight_versions->spotlight_id = $iSpotlightId;
				
				$this->spotlight_versions->deactivate_versions();
				
				$this->spotlight_versions->clear();
			}
			
		}
		
		
		$this->spotlight_versions->spotlight_id = $iSpotlightId;
		$this->spotlight_versions->user_id = $this->user->userId;
		$this->spotlight_versions->version_epoch = $this->iTimeEpoch;
		$this->spotlight_versions->version_active = $sVersionActive; 
		$this->spotlight_versions->version_num = $iVersionNum;
		
		$iVersionId = $this->spotlight_versions->save();
		
		
		$this->aData['json_encode'] = array('iVersionId' => $iVersionId);
		
		$this->load->view('php_json_encode', $this->aData);
		
		
		
	}



	public function add_images_to_version()
	{
		$iVersionId = $this->input->post('iVersionId', true, 0);
		$iMediaId = $this->input->post('iMediaId', true, 0);
		$iImagePos = $this->input->post('iImagePos', true, 0);
		
		
		$this->load->model('db/spotlight_images', 'spotlight_images');
		
		$this->spotlight_images->version_id = $iVersionId;
		$this->spotlight_images->media_id = $iMediaId;
		$this->spotlight_images->image_pos = $iImagePos;
		
		$iImageId = $this->spotlight_images->save();
		
		
		$this->aData['json_encode'] = array('iImageId' => $iImageId);
		
		$this->load->view('php_json_encode', $this->aData);
			
	}
	
	
	
	public function add_text_to_image()
	{
		$iImageId = $this->input->post('iImageId', true, 0);
		$sTextHeader = $this->input->post('sTextHeader', true, '');
		$sTextContent = $this->input->post('sTextContent', true, '');
		$iTextPos = $this->input->post('iTextPos', true, 0);
		$iPageId = $this->input->post('iPageId', true, null);
		
		if ($iPageId == '0') 
		{
			$iPageId = null;	
		}
		
		$this->load->model('db/spotlight_text', 'spotlight_text');
		
		$this->spotlight_text->image_id = $iImageId;
		$this->spotlight_text->text_header = $sTextHeader;
		$this->spotlight_text->text_content = $sTextContent;
		$this->spotlight_text->text_pos = $iTextPos;
		$this->spotlight_text->page_id = $iPageId;
		
		$iTextId = $this->spotlight_text->save();
		
		$this->aData['json_encode'] = array('iTextId' => $iTextId);
		
		$this->load->view('php_json_encode', $this->aData);

	}
}

?>